package com.mani.geotag;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class GetMapTask {
	
	public interface MapDownloadedCallback {
		public void onMapDownlaoded();
	}
	
	public Bitmap mBitmap4x2;
	public Bitmap mBitmap2x2;
	Context mContext;
	MapDownloadedCallback mCB;
	
	GetMapTask(MapDownloadedCallback aCB){
		mCB = aCB;
	}
	
	double mLat;
	double mLong;
	
	public void fetchMap(double lat, double longg) {
		
		mLat = lat;
		mLong = longg;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                	downloadBitmap2x2("http://maps.googleapis.com/maps/api/staticmap?center="+mLat+","+mLong+"&markers=color:blue|label:S|"+mLat+","+mLong+"&zoom=16&size=147x142&scale=2&sensor=false");
                	downloadBitmap4x2("http://maps.googleapis.com/maps/api/staticmap?center="+mLat+","+mLong+"&markers=color:blue|label:S|"+mLat+","+mLong+"&zoom=16&size=297x142&scale=2&sensor=false");
                    mCB.onMapDownlaoded();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //dialog.dismiss();
                }
            }
        }).start();
 
    }
 
    private Bitmap downloadBitmap2x2(String api) throws IOException {
    	
    	System.out.println("Downoadbitmap is api ******* "+api);
    	URL url=null;
    	URI uri= null;
    	try {
			url = new URL(api);
			uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
    	} catch (URISyntaxException e) {
    		return null;
    	}
    	
        HttpUriRequest request = new HttpGet(uri);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
 
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            byte[] bytes = EntityUtils.toByteArray(entity);
 
            mBitmap2x2 = BitmapFactory.decodeByteArray(bytes, 0,
                    bytes.length);
            return mBitmap2x2;
        } else {
            throw new IOException("Download failed, HTTP response code "
                    + statusCode + " - " + statusLine.getReasonPhrase());
        }
    }
    
    private Bitmap downloadBitmap4x2(String api) throws IOException {
    	
    	System.out.println("Downoadbitmap is api ******* "+api);
    	URL url=null;
    	URI uri= null;
    	try {
			url = new URL(api);
			uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
    	} catch (URISyntaxException e) {
    		return null;
    	}
    	
        HttpUriRequest request = new HttpGet(uri);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
 
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            byte[] bytes = EntityUtils.toByteArray(entity);
 
            mBitmap4x2 = BitmapFactory.decodeByteArray(bytes, 0,
                    bytes.length);
            return mBitmap4x2;
        } else {
            throw new IOException("Download failed, HTTP response code "
                    + statusCode + " - " + statusLine.getReasonPhrase());
        }
    }

}
